import time
import os

from tornado import web, ioloop


class MainHandler(web.RequestHandler):

    # 当客户端发起不同的http方法的时候，只需要重载handler中对应的方法即可
    async def get(self, *args, **kwargs):
        word = "hello zhangsan"
        self.render('hello.html', word=word)


class OrdersHandler(web.RequestHandler):

    def cal_total(self, order):
        price = order.get('price', 0)
        num = order.get('nums', 0)
        return price * num

    async def get(self, *args, **kwargs):
        orders = [
            {
                "name": "小米T恤 忍者米兔双截棍 军绿 XXL <b>这段文本加粗</b>",
                "image": "http://i1.mifile.cn/a1/T11lLgB5YT1RXrhCrK!40x40.jpg",
                "price": 39,
                "nums": 3
            },
            {
                "name": "招财猫米兔 白色",
                "image": "http://i1.mifile.cn/a1/T14BLvBKJT1RXrhCrK!40x40.jpg",
                "price": 49,
                "nums": 2
            },
            {
                "name": "小米圆领纯色T恤 男款 红色 XXL",
                "image": "http://i1.mifile.cn/a1/T1rrDgB4DT1RXrhCrK!40x40.jpg",
                "price": 59,
                "nums": 1
            },
        ]

        self.render('order.html',
                    orders=orders,
                    cal_total=self.cal_total
                    )


class OrdersTemHandler(web.RequestHandler):

    def cal_total(self, order):
        price = order.get('price', 0)
        num = order.get('nums', 0)
        return price * num

    async def get(self, *args, **kwargs):
        orders = [
            {
                "name": "小米T恤 忍者米兔双截棍 军绿 XXL <b>这段文本加粗</b>",
                "image": "http://i1.mifile.cn/a1/T11lLgB5YT1RXrhCrK!40x40.jpg",
                "price": 39,
                "nums": 3
            },
            {
                "name": "招财猫米兔 白色",
                "image": "http://i1.mifile.cn/a1/T14BLvBKJT1RXrhCrK!40x40.jpg",
                "price": 49,
                "nums": 2
            },
            {
                "name": "小米圆领纯色T恤 男款 红色 XXL",
                "image": "http://i1.mifile.cn/a1/T1rrDgB4DT1RXrhCrK!40x40.jpg",
                "price": 59,
                "nums": 1
            },
        ]

        self.render('order.html',
                    orders=orders,
                    cal_total=self.cal_total
                    )


class SleepHandler(web.RequestHandler):

    async def get(self, *args, **kwargs):
        # 在编写中不能在里面写同步IO逻辑
        time.sleep(10)
        self.write("sleep")


if __name__ == '__main__':
    base_dir = os.path.dirname(__file__)
    setting = {
        # 设置静态文件路径，默认前缀是 static  => http://xxx.com/static/1.png
        "static_path": os.path.join(base_dir, 'static'),
        'static_url_prefix': "/static/",  # 设置静态文件路径前缀
        'template_path': os.path.join(base_dir, 'templates')
    }

    app = web.Application([
        ("/", MainHandler),
        ("/2/", web.RedirectHandler, {'url': "/"}),  # 自动跳转到/路径，适合当该连接废弃的时候，但是业务原因该连接不能废除
        ("/static3/(.*)", web.StaticFileHandler, {"path": os.path.join(os.path.dirname(__file__), 'static')}),
        # 这样也可以达到静态文件访问的目的
        ('/orders/?', OrdersHandler),
        ('/order/?', OrdersTemHandler)
    ], debug=True, **setting)

    app.listen(8000)

    ioloop.IOLoop.current().start()
